package com.igeek.blog_parent_test.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.blog_parent_test.constant.MessageConstant;
import com.igeek.blog_parent_test.entity.Articles;
import com.igeek.blog_parent_test.entity.Users;
import com.igeek.blog_parent_test.result.Result;
import com.igeek.blog_parent_test.service.IArticlesService;
import com.igeek.blog_parent_test.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-04
 */
@RestController
@RequestMapping("/admin/articles")
@Api(tags = "文章的接口管理")
public class ArticlesController {
    @Autowired
    private IArticlesService IArticlesService;

    @ApiOperation("分页查询文章")
    @GetMapping("findPage")
    public Result findPage(@ApiParam("分页参数") PageQueryVO vo) {
        Page<Articles> page = IArticlesService.findPage(vo);
        if (page != null) {
            return new Result(true, MessageConstant.QUERY_ARTICLES_SUCCESS, page);
        }
        return new Result(false, MessageConstant.QUERY_ARTICLES_FAIL);
    }

    @ApiOperation("新增文章")
    @PostMapping("/add")
    public Result add(@RequestBody Articles articles) {
        boolean flag = IArticlesService.save(articles);
        if (flag) {
            return new Result(true, MessageConstant.ADD_ARTICLES_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_ARTICLES_FAIL);
    }
    @ApiOperation("通过ID查询文章信息")
    @GetMapping("{article_id}")
    public Result findOne(@PathVariable("article_id")Integer article_id){
        Articles articles=IArticlesService.getById(article_id);
        if(ObjectUtils.isEmpty(articles)) {
            return new Result(true, MessageConstant.QUERY_ARTICLES_SUCCESS,articles);
        }
        return new Result(true, MessageConstant.QUERY_ARTICLES_FAIL);
    }
    @ApiOperation("编辑文章")
    @PostMapping("update")
    public Result update(@RequestBody Articles articles){
        boolean flag = IArticlesService.updateById(articles);
        if(flag) {
            return new Result(true, MessageConstant.EDIT_ARTICLES_SUCCESS);
        }
        return new Result(false,MessageConstant.EDIT_ARTICLES_FAIL);
    }
    @ApiOperation("删除文章")
    @DeleteMapping("/{article_id}")
    public Result delete(@PathVariable("article_id")Integer article_id){
        boolean flag = IArticlesService.del(article_id);
        if(flag){
            return new Result(true, MessageConstant.DELETE_ARTICLES_SUCCESS);
        }
        return new Result(true, MessageConstant.DELETE_ARTICLES_FAIL);
    }
}
